package com.blog.service.Impl;

import java.util.List;

import com.blog.BO.PostBO;
import com.blog.BO.UserBO;
import com.blog.dao.AllPostDao;
import com.blog.dao.CheckPostDao;
import com.blog.dao.CheckUserDao;
import com.blog.dao.MultilistDao;
import com.blog.dao.UserDao;
import com.blog.dao.impl.AllPostDaoImpl;
import com.blog.dao.impl.CheckPostDaoImpl;
import com.blog.dao.impl.CheckUserDaoImpl;
import com.blog.dao.impl.MultilistDaoImpl;
import com.blog.dao.impl.UserDaoImpl;
import com.blog.entity.Page;
import com.blog.entity.Post;
import com.blog.entity.User;
import com.blog.service.UserService;

public class UserServiceImpl implements UserService {
	private UserDao ud= new UserDaoImpl();
	private AllPostDao ap = new AllPostDaoImpl();
	private MultilistDao mld = new MultilistDaoImpl();
	private CheckUserDao cud = new CheckUserDaoImpl();
	private CheckPostDao cpd = new CheckPostDaoImpl();
	@Override
	public int insterUser(User user) {
		// TODO Auto-generated method stub
		User user1= ud.selectUserByUserName(user.getUserName());
		if(user1 !=null) {
			return -1;
		}
		UserBO pb= new UserBO();
		pb.setUserName(user.getUserName());
		List<User> data=cud. selectCheckUser(pb, 1, 1);
		User u=data.get(0);
		int num1=u.getUserState();
		if(num1==1) {
			cud.deleteCheckUserByUserId(u.getUserId());
			return ud.insertUser(u);
		}
		return 0;
	}

	@Override
	public User selectUserByName(String username, String password) {
		// TODO Auto-generated method stub
		return ud.selectUserByUserNameAndUserPass(username, password);
	}

	@Override
	public int updateById(User user) {
		// TODO Auto-generated method stub
		return ud.updateUser(user);
	}

	@Override
	public User selectById(int userId) {
		// TODO Auto-generated method stub
		return ud.selectUserByUserId(userId);
	}

	@Override
	public int deleteById(int userId) {
		// TODO Auto-generated method stub\
		
		return ud.deleteUserByUserId(userId);
	}

	@Override
	public Page<Post> selectAllPostById(int userId, int pageNum, int pageSize) {
		// TODO Auto-generated method stub
		List<Post> data = ap.selectAllPostByPostUserId(userId,pageNum,pageSize);
		int total =ap.selectPostNum();
		return new Page<Post>(data, pageNum, pageSize, total);
	}

	@Override
	public int selectAllPostById(int userId) {
		// TODO Auto-generated method stub
		PostBO pb= new PostBO();
		pb.setPostUserId(userId);
		List<Post> data=cpd.selectCheckPost(pb, 1, 1);
		Post pt=data.get(0);
		return pt.getPostState();
	}

	@Override
	public Page<User> selectByLikeId(int likeId, int pageNum, int pageSize) {
		// TODO Auto-generated method stub
		List<User> data = mld.selectUserFansByLikeId(likeId,pageNum,pageSize);
		return new Page<User>(data, pageNum, pageSize, 20);
	}

	@Override
	public Page<User> selectByUserId(int userId, int pageNum, int pageSize) {
		// TODO Auto-generated method stub
		List<User> data = mld.selectUserLikeByUserId(userId,pageNum,pageSize);

		return new Page<User>(data, pageNum, pageSize, 20);
	}

	@Override
	public Page<User> selectUsercheck(int pageNum, int pageSize) {
		// TODO Auto-generated method stub
		List<User> data = cud.selectCheckUser(null, pageNum, pageSize);
		int total =cud.selectCheckUserNum();
		return new Page<User>(data, pageNum, pageSize, total);
	}

}
